<?php

class Job_model extends Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::Model();
		$this->_table = $this->config->item('database_tables');
	}
	
	// Public methods
	public function get_job($id)
	{
		if($id == null)
		{
			return null;
		}
		else
		{
			$this->db->select('*');
			$this->db->where('id',$id);
			$query = $this->db->get($this->_table['job']. ' job');
			if($query->num_rows()>0)
			{
				$result = $query->row_array();
				return $result;
			}
			else
			{
				return null;
			}
		}
	}
	public function get_jobs($lang_id=null)
	{
		$this->db->select('*');
		if($lang_id)
		{
			$this->db->where('lang_id',$lang_id);
		}
		$this->db->order_by('id', 'DESC');
			
		$query = $this->db->get($this->_table['job'] . ' job');
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
		return null;
	}
	public function get_pass_job()
	{
		
	}
	public function create_job($lang_id=null,$title='')
	{
		$creator_id = $this->session->userdata('user_id');
		$created_date = ($this->input->post('created_date') != '') ? date('Y-m-d', strtotime($this->input->post('created_date'))) : date('Y-m-d');
		$ended_date = ($this->input->post('ended_date') != '') ? date('Y-m-d', strtotime($this->input->post('ended_date'))) : date('Y-m-d');
		$job_url = url_title($this->input->post('title'), 'dash', TRUE);
		$status = 1;
		$data = array(
				'title'=>($title) ? $title:$this->input->post('title'),
				'content'=>html_entity_decode($this->input->post('fck_content'), ENT_NOQUOTES, 'UTF-8'),
				'excerpt'=>html_entity_decode($this->input->post('fck_excerpt'), ENT_NOQUOTES, 'UTF-8'),
				'creator_id'=>$creator_id,
				'category_id'=>0,
				'status'=>$status,
				'lang_id'=>($lang_id) ? $lang_id:2,
				'order'=>0,
				'started_date'=>$created_date,
				'ended_date'=>$ended_date,
				'job_url'=>$job_url
		);
		$this->db->insert($this->_table['job'], $data);
	}
	public function edit_job($id)
	{
		$created_date = ($this->input->post('created_date') != '') ? date('Y-m-d', strtotime($this->input->post('created_date'))) : date('Y-m-d');
		$ended_date = ($this->input->post('ended_date') != '') ? date('Y-m-d', strtotime($this->input->post('ended_date'))) : date('Y-m-d');
		$status = ($this->input->post('status') == 1) ? '1' : '0';
		$data = array(
				'title'=>($title) ? $title:$this->input->post('title'),
				'content'=>html_entity_decode($this->input->post('fck_content'), ENT_NOQUOTES, 'UTF-8'),
				'excerpt'=>html_entity_decode($this->input->post('fck_excerpt'), ENT_NOQUOTES, 'UTF-8'),
				'category_id'=>0,
				'status'=>$status,
				'order'=>0,
				'started_date'=>$created_date,
				'ended_date'=>$ended_date
				
		);
		$this->db->where('id', $id);
		$this->db->update($this->_table['job'], $data);
	}
	public function delete_job($id)
	{
		$this->db->where('id', $id);
		$this->db->delete($this->_table['job']);	
	}
}

/* End of file job_model.php */